package wowraidmanager.client.wishlist.contact;

import wowraidmanager.client.BaseComposite;
import wowraidmanager.client.service.wishlist.contact.ContactService;
import wowraidmanager.client.service.wishlist.contact.ContactServiceAsync;
import wowraidmanager.client.wishlist.utils.CheckError;
import wowraidmanager.shared.model.wishlist.UserStatus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Contact extends BaseComposite{

    private static ContactUiBinder uiBinder = GWT.create(ContactUiBinder.class);

    protected ContactServiceAsync contactService = GWT.create(ContactService.class);

    UserStatus userStatus;

    @UiField
    TextBox titleText;

    @UiField
    RichTextArea contentText;

    @UiField
    Button sendButton;

    @UiField
    Button cancelButton;

    interface ContactUiBinder extends UiBinder<Widget, Contact>{
    }

    public Contact(UserStatus userStatus){
        initWidget(uiBinder.createAndBindUi(this));
        this.userStatus = userStatus;
        setButton();
    }

    private void setButton(){
        sendButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setMail();

            }
        });

        cancelButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                clearText();

            }
        });
    }

    protected void setMail(){
        String mailContent = contentText.getHTML();
        errorString = CheckError.checkMailContent(mailContent);

        if(errorString.equals("")){
            contactService.sendMail(titleText.getText(), mailContent, userStatus.getMember(), new AsyncCallback<Void>(){

                public void onSuccess(Void result){
                    setCheckMail();
                }

                public void onFailure(Throwable caught){
                    clearInfoMessage();
                    setErrorMessage("メールの送信に失敗しました。");
                }
            });
        }else{
            clearInfoMessage();
            setErrorMessage(errorString);
        }
    }

    protected void setCheckMail(){
        contactService.sendContactCheckMail(titleText.getText(), contentText.getHTML(), userStatus.getMember(), new AsyncCallback<Void>(){

            public void onSuccess(Void result){
                clearText();
                setInfoMessage("メールの送信に成功しました。<br/>確認のメールが送信されます。");
                clearErrorMessage();
            }

            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage("メールの送信に失敗しました。");
            }
        });

    }

    protected void clearText(){
        titleText.setText("");
        contentText.setText("");
    }

}
